/*
* File:		support_common.h
 * Purpose:		Various project configurations.
 *
 * Notes:
 */

#ifndef _SUPPORT_COMMON_H_
#define _SUPPORT_COMMON_H_

// Include
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"
#include "PE_Timer.h"

#include "speed_control.h"
#include "linear_ccd.h"
#include "line_detection.h"
#include "lcd.h"
#include "lcd_gui.h"
#include "qspi.h"
#include "ADC.h"


#define MEMORY_INIT \
	/* Initialize RAMBAR: locate SRAM and validate it */ \
	move.l	%#__RAMBAR + 0x21,d0; \
	movec	d0,RAMBAR;


/* Private Define */
#define ONE_SYS_LOOP_TIME_CONSTANT	16

#define TRUE				1
#define FALSE				0

#define MOTOR_PROTECTION	TRUE
#define STOP_WHEN_BLACK		TRUE
#define	NUM_OF_STATE		20

#define	MOTOR_MIDDLE		250
#define GO_FORWARD			0
#define GO_BACKWARD			1

#define SERVO_MIDDLE		1635
#define SERVO_AMPLITUDE		250
#define SERVO_MAX			SERVO_MIDDLE + SERVO_AMPLITUDE
#define SERVO_MIN			SERVO_MIDDLE - SERVO_AMPLITUDE

#define Kp					0
#define Ki					1
#define	Kd					2
#define _P					0
#define _I					1
#define	_D					2
#define CURRENT				0
#define PREV				1
#define PREV_2				2

#define LED_ON				0
#define LED_OFF				1


/* Private typedef*/
typedef struct PID_control 
{
  short set_value;
  float constant[3];
  int controller[3];
  int error[3];
  int integral;
  int derivative;
} PID_control;

typedef enum commandType 
{
	NONE,
	COLLECT_CCD,
	SERVO_CONTROL,
	SPEED_CONTROL,
	STOP,
	STOPPING_LINE
} commandType;

typedef enum GPIO_Pin 
{
	_0, _1, _2, _3, _4, _5, _6, _7, NA, ALL
} GPIO_Pin;

typedef enum SYS_MODE 
{
	MODE0 = 0,
	MODE1 = 2,
	MODE2 = 1,
	MODE3 = 3,
	MODE4 = 4,
	MODE5 = 6,
	MODE6 = 5,
	MODE7 = 7,
	MODE8 = 8,
	MODE9 = 10,
	MODE10 = 9,
	MODE11 = 11,
	MODE12 = 12,
	MODE13 = 14,
	MODE14 = 13,
	MODE15 = 15
} SYS_MODE;

extern volatile commandType sysLoopCmd;
extern volatile commandType sysLoopCmd_prev;
extern volatile PID_control speedPID;
extern volatile PID_control servoPID;
extern uint16 speed_set_value;

extern float a;
extern float b;
extern float kp_for_small_error;
extern short error_threshold;
extern uint16 straightRoad_threshold;


/* Variables */


/* Private Method Declaration */
void InitializeUART();
uint32 ReadUARTN();
uint32 WriteUARTN(unsigned char *buffer, unsigned long count);
void selectMode(SYS_MODE);
__declspec(register_abi) void delay_us(word xus:__D0);
__declspec(register_abi) void delay_10us(word x10us:__D0);


#endif /* _SUPPORT_COMMON_H_ */

